/*-
 * Copyright 2012 Guram Dukashvili
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
//---------------------------------------------------------------------------
#ifndef __mutex_h__
#define __mutex_h__
//---------------------------------------------------------------------------
#include "config.h"
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
struct mutex {
#if HAVE_PTHREAD_H
  pthread_mutex_t handle;
#elif __WIN32__
  HANDLE handle;
#else
#error must be define for arch
#endif

#ifndef NDEBUG
  bool acquired;
#endif
};
//---------------------------------------------------------------------------
void mutex_init_runtime(void);
void mutex_destroy_runtime(void);
struct mutex * mutex_new(void);
void mutex_init(struct mutex * mtx);
void mutex_destroy(struct mutex * mtx);
void mutex_delete(struct mutex * mtx);
void mutex_acquire(struct mutex * mtx);
void mutex_release(struct mutex * mtx);
bool mutex_try_acquire(struct mutex * mtx);
//---------------------------------------------------------------------------
struct semaphore {
#if HAVE_SEMAPHORE_H
  sem_t handle;
#elif __WIN32__
  HANDLE handle;
#endif
  uint32_t initial_value;
};
//---------------------------------------------------------------------------
struct semaphore * semaphore_new(void);
void semaphore_init(struct semaphore * smr);
void semaphore_init_v(struct semaphore * smr,uint32_t initial_value);
void semaphore_destroy(struct semaphore * smr);
void semaphore_delete(struct semaphore * smr);
void semaphore_post(struct semaphore * smr);
void semaphore_post_v(struct semaphore * smr,uintptr_t count);
void semaphore_wait(struct semaphore * smr);
bool semaphore_try_wait(struct semaphore * smr);
bool semaphore_timed_wait(struct semaphore * smr,uint64_t timeout);
//---------------------------------------------------------------------------
static __inline__ void semaphore_acquire(struct semaphore * smr)
{
  semaphore_wait(smr);
}
//---------------------------------------------------------------------------
static __inline__ void semaphore_release(struct semaphore * smr)
{
  semaphore_post(smr);
}
//---------------------------------------------------------------------------
static __inline__ bool semaphore_try_acquire(struct semaphore * smr)
{
  return semaphore_try_wait(smr);
}
//---------------------------------------------------------------------------
struct cond_var {
  struct mutex lock;
  struct semaphore wait;
  uint32_t waiters;
  bool v;
};
//---------------------------------------------------------------------------
struct cond_var * cond_var_new(void);
void cond_var_init(struct cond_var * var);
void cond_var_init_v(struct cond_var * var,bool v);
void cond_var_destroy(struct cond_var * var);
void cond_var_delete(struct cond_var * var);
void cond_var_set(struct cond_var * var,bool v);
void cond_var_wait_equ(struct cond_var * var,bool v);
bool cond_var_wait_equ_timeout(struct cond_var * var,bool v,uint64_t timeout);             // timeout in nano seconds
//---------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
